<template>
  <div>
    <div style="margin-bottom: 5px">
      <el-input placeholder="请输入物品名" style="width: 200px;" suffix-icon="el-icon-search" v-model="name" @keyup.enter.native="loadPost"></el-input>
      <el-select v-model="storage" placeholder="请选择仓库" style="margin-left: 5px">
        <el-option
            v-for="item in storageData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
      <el-select v-model="goodstype" placeholder="请选择物品类名" style="margin-left: 5px">
        <el-option
            v-for="item in goodstypeData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
      <el-button type="primary" style="margin-left: 5px;" @click="loadPost">查询</el-button>
      <el-button type="success" @click="resetParam">重置</el-button>
      <el-button type="primary" style="margin-left: 5px;" @click="add" v-if="user.roleId!=1">新增</el-button>
      <el-button type="primary" style="margin-left: 5px;" @click="ingoods" v-if="user.roleId!=1">入库</el-button>
      <el-button type="primary" style="margin-left: 5px;" @click="outgoods" v-if="user.roleId!=1">出库</el-button>

    </div>
    <el-table :data="tableData"
              :header-cell-style="{ background: '#ced9e3',color: '#555555'}"
              border
              highlight-current-row
              @current-change="selectCurrentChange"


    >

      <el-table-column prop="id" label="ID" width="140">
      </el-table-column>
      <el-table-column prop="name" label="物品名" width="180">
      </el-table-column>
      <el-table-column prop="storage" label="仓库名" width="180":formatter="formatStorage">
      </el-table-column>
      <el-table-column prop="goodstype" label="物品类名" width="180" :formatter="formatGoodstype">
      </el-table-column>
      <el-table-column prop="count" label="数量" width="180">
      </el-table-column>
      <el-table-column prop="remark" label="备注">
      </el-table-column>

      <el-table-column prop="operate" label="操作" v-if="user.roleId!=1">
        <template slot-scope="scope">
          <el-button type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm
              title="确定删除吗？"
              @confirm="del(scope.row.id)"
              style="margin-left: 5px;"
          >
            <el-button slot="reference" type="danger" >删除</el-button>
          </el-popconfirm>

        </template>
      </el-table-column>




    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <el-dialog
        title="提示"
        :visible.sync="centerDialogVisible"
        width="30%"
        center>
      <el-form ref="form":rules="rules" :model="form" label-width="80px">

        <el-form-item label="物品名"prop="name">
          <el-col :span="11">
            <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="仓库名"prop="storage">
          <el-col :span="11">

            <el-select v-model="form.storage" placeholder="请选择仓库" style="margin-left: 5px">
              <el-option
                  v-for="item in storageData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="分类"prop="goodstype">
          <el-col :span="11">
            <el-select v-model="form.goodstype" placeholder="请选择物品类名" style="margin-left: 5px">
              <el-option
                  v-for="item in goodstypeData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="数量"prop="count">
          <el-col :span="11">
            <el-input v-model="form.count"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-col :span="11">

            <el-input type="textarea" v-model="form.remark"></el-input>
          </el-col>
        </el-form-item>





      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
    </el-dialog>

    <el-dialog
        title="提示"
        :visible.sync="centerDialogVisible"
        width="30%"
        center>
      <el-form ref="form":rules="rules" :model="form" label-width="80px">

        <el-form-item label="物品名"prop="name">
          <el-col :span="11">
            <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="仓库名"prop="storage">
          <el-col :span="11">

            <el-select v-model="form.storage" placeholder="请选择仓库" style="margin-left: 5px">
              <el-option
                  v-for="item in storageData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="分类"prop="goodstype">
          <el-col :span="11">
            <el-select v-model="form.goodstype" placeholder="请选择物品类名" style="margin-left: 5px">
              <el-option
                  v-for="item in goodstypeData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="数量"prop="count">
          <el-col :span="11">
            <el-input v-model="form.count"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-col :span="11">

            <el-input type="textarea" v-model="form.remark"></el-input>
          </el-col>
        </el-form-item>





      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
    </el-dialog>

  <!-- 入库的弹窗-->
    <el-dialog
        title="出入库"
        :visible.sync="inDialogVisible"
        width="30%"
        center>
        <el-dialog
            width="60%"
            title="选择申请人"
            :visible.sync="innerVisible"
            append-to-body>
          <SelectUser @doSelectUser="doSelectUser"></SelectUser>
          <span slot="footer" class="dialog-footer">
                <el-button @click="innerVisible = false">取 消</el-button>
                <el-button type="primary" @click="comfirmUser">确 定</el-button>
          </span>

        </el-dialog>
      <el-form ref="form1":rules="rules1" :model="form1" label-width="80px">

        <el-form-item label="物品名">
          <el-col :span="11">
            <el-input v-model="form1.goodsname" readonly></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="申请人">
          <el-col :span="11">
            <el-input v-model="form1.username" readonly @click.native="selectUser"></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="数量"prop="count">
          <el-col :span="11">
            <el-input v-model="form1.count"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-col :span="11">

            <el-input type="textarea" v-model="form1.remark"></el-input>
          </el-col>
        </el-form-item>





      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="doInGoods">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>

import SelectUser from "@/components/user/SelectUser";
export default {
  name: "GoodsManage",
  components: {SelectUser},
  data() {
    let checkCount = (rule, value, callback) => {
      if(value>9999){
        callback(new Error('数量输⼊过⼤'));
      }else{
        callback();
      }
    };


    return {
      user : JSON.parse(sessionStorage.getItem('CurUser',)),
      goodstypeData:[],
      storageData:[],
      tableData: [],
      pageSize:5,
      pageNum:1,
      total:0,
      name:'',
      storage: '',
      goodstype: '',
      centerDialogVisible:false,
      inDialogVisible:false,
      innerVisible:false,
      currentRow:{},
      tempUser:{},
      form:{
        id:'',
        name:'',
        storage:'',
        goodstype:'',
        count:'',
        remark:'',


      },
      form1:{
        goods:'',
        goodsname:'',
        count:'',
        remark:'',
        userid:'',
        username:'',
        adminId:'',
        action:'1',
      },
      rules1:{},
      rules: {

        name: [
          {required: true, message: '请输入物品名', trigger: 'blur'},
        ],
        storage: [
          {required: true, message: '请选择仓库', trigger: 'blur'},
        ],
        goodstype: [
          {required: true, message: '请选择物品类别', trigger: 'blur'},
        ],
        count: [
          {required: true, message: '请输⼊数量', trigger: 'blur'},
          {pattern: /^([1-9][0-9]*){1,4}$/,message: '数量必须为正整数字',trigger: "blur"},
          {validator:checkCount,trigger: 'blur'}
        ],

      }
    }
  },
  methods:{
    doSelectUser(val){
      this.tempUser = val

    },
    comfirmUser(){
      this.form1.username = this.tempUser.name
      this.form1.userid = this.tempUser.id
      this.innerVisible = false

    },

    formatStorage(row){
      let temp = this.storageData.find(item=>{
        return item.id == row.storage
      })
      return temp && temp.name

    },
    formatGoodstype(row){
      let temp = this.goodstypeData.find(item=>{
        return item.id == row.goodstype
      })
      return temp && temp.name

    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },

    loadPost(){
      this.$axios.post('http://localhost:8090/goods/listPage1',{
        pageSize:this.pageSize,
        pageNum:this.pageNum,
        param:{
          name:this.name,
          goodstype: this.goodstype+'',
          storage: this.storage+''
        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.tableData=res.data
          this.total=res.total
        }
        else{
          alert('获取数据失败')
        }
      })
    },
    loadStorage(){
      this.$axios.get('http://localhost:8090/storage/list').then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.storageData=res.data
        }
        else{
          alert('获取数据失败')
        }
      })
    },
    loadGoodstype(){
      this.$axios.get('http://localhost:8090/goodstype/list').then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.goodstypeData=res.data
        }
        else{
          alert('获取数据失败')
        }
      })
    },
    resetParam(){
      this.name=''
      this.storage=''
      this.goodstype=''
    },
    add(){
      this.centerDialogVisible = true;
      this.$nextTick(()=>{
        this.resetForm()
      })/*清空表单*/


    },
    ingoods(){
      if(!this.currentRow.id){
        alert("请选择物品🙂")
        return;
      }
      this.inDialogVisible = true;
      this.$nextTick(()=>{
        this.resetInForm()
      })/*清空表单*/
      this.form1.goodsname = this.currentRow.name;
      this.form1.goods = this.currentRow.id;
      this.form1.adminId = this.user.id;
      this.form1.action = 1;


    },
    outgoods(){
      if(!this.currentRow.id){
        alert("请选择物品🙂")
        return;
      }
      this.inDialogVisible = true;
      this.$nextTick(()=>{
        this.resetInForm()
      })/*清空表单*/
      this.form1.goodsname = this.currentRow.name;
      this.form1.goods = this.currentRow.id;
      this.form1.adminId = this.user.id;
      this.form1.action = 2;


    },
    resetInForm(){

      this.$refs.form1.resetFields();
    },
    selectCurrentChange(val){
      this.currentRow = val;

    },
    doMod(){
      this.$axios.post(this.$httpUrl+'/goods/update',this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.$message({
            message: '修改成功',
            type: 'success'
          });
          this.centerDialogVisible = false;
          this.loadPost()
        }
        else{
          this.$message({
            message: '修改失败(T_T)',
            type: 'error'
          });
          this.centerDialogVisible = false;
        }
      })

    },
    doSave(){
      this.$axios.post(this.$httpUrl+'/goods/save',this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.$message({
            message: '添加成功',
            type: 'success'
          });
          this.centerDialogVisible = false;
          this.loadPost()
        }
        else{
          this.$message({
            message: '添加失败(T_T)',
            type: 'error'
          });
          this.centerDialogVisible = false;
        }
      })

    },
    save(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          if(this.form.id)
          {
            this.doMod();
          }
          else{
            this.doSave();
          }


        } else {
          console.log('error submit!!');
          return false;
        }
      });



    },
    resetForm(formName) {
      this.$refs.form.resetFields();
    },
    del(id){
      console.log(id)
      this.$axios.get(this.$httpUrl+'/goods/del?id='+id).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.loadPost()
        }
        else{
          this.$message({
            message: '删除失败(T_T)',
            type: 'error'
          });

        }
      })

    },
    mod(row){
      console.log(row)
      this.centerDialogVisible= true
      this.$nextTick(()=>{
        this.form.id=row.id
        this.form.name=row.name
        this.form.storage=row.storage
        this.form.goodstype=row.goodstype
        this.form.count=row.count
        this.form.remark=row.remark


      })/*赋值到表单*/





    },
    doInGoods(){

      this.$axios.post(this.$httpUrl+'/record/save',this.form1).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.$message({
            message: '添加成功',
            type: 'success'
          });
          this.inDialogVisible = false;
          this.loadPost()
          this.resetInForm()
        }
        else{
          this.$message({
            message: '添加失败(T_T)',
            type: 'error'
          });
          this.centerDialogVisible = false;
        }
      })

    },
    selectUser(){
       this.innerVisible= true;

    },


  },
  beforeMount() {
    this.loadStorage();
    this.loadGoodstype();

    this.loadPost();

  }
}
</script>

<style scoped>

</style>
